package com.anXin.user.service.impl;

import com.anXin.model.common.dtos.ResponseResult;
import com.anXin.model.common.enums.AppHttpCodeEnum;
import com.anXin.model.user.pojos.Addr;

import com.anXin.model.user.pojos.User;
import com.anXin.user.mapper.AddrMapper;

import com.anXin.user.service.AddrService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import utils.thread.AppThreadLocalUtil;

import java.util.List;


@Service("addrService")
public class AddrServiceImpl extends ServiceImpl<AddrMapper, Addr> implements AddrService {


    @Override
    public ResponseResult add(Addr addr) {
        User user = AppThreadLocalUtil.getUser();
        if (user==null)return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        addr.setUserId(user.getId());
        boolean save = save(addr);
        if (save)return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult updateEquipment(Addr addr) {
        boolean updateById = updateById(addr);
        if (updateById)return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult delete(Long id) {
        boolean removeById = removeById(id);
        if (removeById)return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult getList() {
        User user = AppThreadLocalUtil.getUser();
        if (user==null)return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        List<Addr> addrs = query().eq("user_id", user.getId()).list();
        if (addrs.isEmpty())return ResponseResult.errorResult(AppHttpCodeEnum.ADDR_LIST_NULL);
        return ResponseResult.okResult(addrs);
    }

    @Override
    public ResponseResult Info(Long id) {
        return ResponseResult.okResult(query().eq("id",id).one());
    }
}